﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using BeatEmUp.Items.Weapons;
using BeatEmUp.Enums;
using System.Diagnostics;

namespace BeatEmUp.Characters.Enemies
{
    class EnemiesManager
    {
        private EnemiesFactory enemiesFactory = new EnemiesFactory();
        private Random rand;
        private ContentManager contentManager;
        private List<Enemies> enemiesList = new List<Enemies>();
        private GameTime gameTime;

        internal List<Enemies> EnemiesList
        {
            get { return enemiesList; }
            set { enemiesList = value; }
        }

        public EnemiesManager(ContentManager contentManager)
        {
            rand = new Random();
            this.contentManager = contentManager;
        }

        public void GiveMeRandomEnemy(int playerLevel,bool boss)//Zmieniłem tak, żeby losowało położenie potwora
        {
            //Enemies tmpEnemy = enemiesFactory.CreateEnemy(playerLevel, rand.Next(0, 4), contentManager, new Vector2(gameField.Width / 2, gameField.Height), gameTime);
            //EnemiesList.Add(tmpEnemy);
            //tmpEnemy = enemiesFactory.CreateEnemy(playerLevel, rand.Next(0, 4), contentManager, new Vector2(gameField.Width + 70, rand.Next(gameField.Height - 86) + gameField.Y), gameTime);
            //EnemiesList.Add(tmpEnemy);
            //tmpEnemy = enemiesFactory.CreateEnemy(playerLevel, rand.Next(0, 4), contentManager, new Vector2(gameField.Width / 2, gameField.Y + 10), gameTime);
            //EnemiesList.Add(tmpEnemy);
            //tmpEnemy = enemiesFactory.CreateEnemy(playerLevel, rand.Next(0, 4), contentManager, new Vector2(-10, rand.Next(gameField.Height - 86) + gameField.Y), gameTime);
            //EnemiesList.Add(tmpEnemy);
            Rectangle gameField = new Rectangle(0, 400, 1024, 768 - 400);
            if (boss)
            {
                Enemies tmpEnemy = enemiesFactory.CreateEnemy(playerLevel, rand.Next(0, 4), contentManager, new Vector2(gameField.Width + 70, gameField.Height - 66 + gameField.Y), gameTime);
                EnemiesList.Add(tmpEnemy);
                tmpEnemy = enemiesFactory.CreateEnemy(playerLevel, rand.Next(0, 4), contentManager, new Vector2(-10, rand.Next(gameField.Height - 66) + gameField.Y), gameTime);
                EnemiesList.Add(tmpEnemy);
                tmpEnemy = enemiesFactory.CreateEnemy(playerLevel, rand.Next(0, 4), contentManager, new Vector2(gameField.Width + 70, gameField.Height - 86 + gameField.Y), gameTime);
                EnemiesList.Add(tmpEnemy);
                tmpEnemy = enemiesFactory.CreateEnemy(playerLevel, rand.Next(0, 4), contentManager, new Vector2(-10, rand.Next(gameField.Height - 86) + gameField.Y), gameTime);
                EnemiesList.Add(tmpEnemy);
            }
            else if (!boss)
            {
                Enemies tmpEnemy = enemiesFactory.CreateEnemy(playerLevel, 5, contentManager, new Vector2(gameField.Width + 70, gameField.Height - 86 + gameField.Y), gameTime);
                EnemiesList.Add(tmpEnemy);
            }
        }

        public void GetCloserToPlayer(Rectangle playerPosition,Player player)
        {
            foreach (Enemies enemy in enemiesList)
            {
                if(enemy.HP>0)enemy.MoveToPlayer(playerPosition,player);

            }
            foreach (Enemies enemyCheck in enemiesList)
            {
                foreach (Enemies enemy in enemiesList)
                {
                    if (enemy.HP > 0) enemyCheck.checkEnemyPosition(enemy.CollisionRec);

                }
            }
        }

        //Update wykorzystane tylko przy podświetlaniu przeciwnika
        public void Update(GameTime gameTime,WeaponsManager weapons,Weapon playerWeapon,Player player)
        {
            this.gameTime = gameTime;
            foreach (Enemies enemy in enemiesList)
            {
                enemy.Update(gameTime);
            }
            RemoveEnemy(weapons,playerWeapon,player);
        }
        private void RemoveEnemy(WeaponsManager weapons,Weapon playerWeapon, Player player)
        {
            for (int i = enemiesList.Count - 1; i >= 0; i--)
            {
                if (enemiesList[i].HP == 0)
                {
                    int result = rand.Next(100);
                    if(result <50)
                        weapons.GenerateNewWeapon(enemiesList[i].GetLevel(), MonsterType.Normal, enemiesList[i].GetPosition(),playerWeapon);
                    else
                    {
                        double round = Math.Round(enemiesList[i].GetLevel()/10.0)+1;
                        Debug.WriteLine("HP multiplier: " + round.ToString());
                        player.HP += 50*(int)round;
                        if (player.HP > player.MaxHP)
                            player.HP = player.MaxHP;
                    }
                    enemiesList.RemoveAt(i);
                }
            }
        }
    }
}
